package com.rmxx.fruit.fragment;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.company.android.fruit.R;
import com.kwan.xframe.common.widget.observablescrollview.ObservableRecyclerView;
import com.kwan.xframe.mvp.view.fragment.BasePageItemFragment;
import com.rmxx.fruit.activity.MainActivity;
import com.rmxx.fruit.activity.OrderListActivity;
import com.rmxx.fruit.activity.ShopActivity;
import com.rmxx.fruit.bean.Product;

import java.util.ArrayList;
import java.util.List;

public class ShopCarFragment extends BasePageItemFragment {

	ObservableRecyclerView mRecyclerView;
	List<Product> mProduct = new ArrayList<>();
	TextView tv_pay;
	private BaseQuickAdapter<Product, BaseViewHolder> mBaseAdapter;

	public ShopCarFragment() {
	}


	// TODO: Rename and change types and number of parameters
	public static ShopCarFragment newInstance() {
		ShopCarFragment fragment = new ShopCarFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
		}
		Product product = new Product();
		product.setShopName("极之鲜食品专营店");
		product.setRicon(R.mipmap.product4_01);
		product.setName("【2袋】张君雅拉面丸子100g");
		product.setDesc("我的火辣不要轻易挑战哦");
		product.setPrice(6.66f);
		mProduct.add(product);


		product = new Product();
		product.setShopName("王小二旗舰店");
		product.setRicon(R.mipmap.product2_04);
		product.setName("麻辣小龙虾香辣鲜活现做");
		product.setDesc("");
		product.setPrice(69.88f);
		mProduct.add(product);

		product = new Product();
		product.setShopName("奇异之家旗舰店");
		product.setRicon(R.mipmap.product2_02);
		product.setName("佳沛金奇异果");
		product.setDesc("");
		product.setPrice(48.00f);
		mProduct.add(product);

		mRecyclerView = mContentView.findViewById(com.kwan.xframe.R.id.recycleView);
		mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
		mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		mRecyclerView.setHasFixedSize(true);

		//int spanCount = 2;
		//int spacing = 20;
		//mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true));
		//麻痹的 关闭硬件加速 否则 cardView 报错

		mRecyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		mBaseAdapter = new BaseQuickAdapter<Product, BaseViewHolder>(R.layout.list_item_shopcar,mProduct) {
			@Override
			protected void convert(BaseViewHolder helper, final Product item) {

				helper.setText(R.id.tv_shop_name,item.getShopName());
				helper.setText(R.id.tv_product_name,item.getName());
				helper.setText(R.id.tv_price,"¥"+item.getPrice());
				helper.setImageResource(R.id.iv_icon,item.getRicon());

				helper.addOnClickListener(R.id.ll_name);
				helper.setOnCheckedChangeListener(R.id.cb_all, new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						item.setChecked(isChecked);
						mRecyclerView.post(new Runnable() {
							@Override
							public void run() {
								mBaseAdapter.notifyDataSetChanged();
							}
						});
					}
				});

				helper.setOnCheckedChangeListener(R.id.cb_item, new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						item.setChecked(isChecked);
						mRecyclerView.post(new Runnable() {
							@Override
							public void run() {
								mBaseAdapter.notifyDataSetChanged();
							}
						});
					}
				});


				helper.setChecked(R.id.cb_all,item.isChecked());
				helper.setChecked(R.id.cb_item,item.isChecked());

			}
		};


		mBaseAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
			@Override
			public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
				go2Activity(ShopActivity.class,null,false);
			}
		});

		mRecyclerView.setAdapter(mBaseAdapter);
		tv_pay = mContentView.findViewById(R.id.tv_pay);
		tv_pay.setOnClickListener(this);

		mContentView.findViewById(R.id.v_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).vp_content.setCurrentItem(0);
			}
		});

		((CheckBox)mContentView.findViewById(R.id.cb_all)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				for(Product p:mProduct)
					p.setChecked(isChecked);

				mBaseAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	protected int getContentViewId() {
		return R.layout.fragment_shopcar;
	}


	@Override
	public String getPageName() {
		return null;
	}


	@Override
	public void fetchData() {

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if(v==tv_pay){
			go2Activity(OrderListActivity.class,null,false);
		}

	}
}
